package cg.project.interceptor;

import cg.project.util.JWTUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import cg.project.util.JWTUtils;

/**
 * 本来用来演示: 登录的拦截器
 *
 * @author success
 * @date 2024/11/5 14:31:22
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getParameter("token");
        if("null".equals(token)){
            return false;
        }
        //如果传入的是一个非法的token,此处的claims是null
        Claims claims = JWTUtils.checkJWT(token);
        if(claims == null){
            return false;
        }

        return true;//拦截不放行
    }
}
